﻿using System;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Linq;
using Entities;
using DataAccess;
namespace Business
{
    public class LogDataBusiness
    {
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static LogDataBusiness Instance { get; private set; }
        /// <summary>
        /// Initializes the <see cref="LogDataBusiness" /> class.
        /// </summary>
        static LogDataBusiness() { Instance = new LogDataBusiness(); }
        /// <summary>
        /// Gets the log data.
        /// </summary>
        /// <returns></returns>
        public Collection<LogData> GetLogData()
        {
            try
            {
                return LogDataDataAccess.Instance.GetLogData();
            }
            catch (SqlException)
            {
                return null;
            }
        }
        /// <summary>
        /// Gets the log data by id record.
        /// </summary>
        /// <param name="iDRecord">The i D record.</param>
        /// <returns></returns>
        public Collection<LogData> GetLogDataByIdRecord(string iDRecord)
        {
            try
            {
                return new Collection<LogData>(LogDataDataAccess.Instance.GetLogData().Where(c => c.IdRecord.ToString().Equals(iDRecord)).ToList());
            }
            catch (SqlException)
            {
                return null;
            }
        }
        /// <summary>
        /// Gets the session by id record.
        /// </summary>
        /// <param name="iDRecord">The i D record.</param>
        /// <returns></returns>
        public string GetSessionByIdRecord(string iDRecord)
        {
            if (!string.IsNullOrEmpty(iDRecord))
                try
                {
                    var logdata = LogDataDataAccess.Instance.GetLogData().Where(c => c.IdRecord.ToString().Equals(iDRecord,StringComparison.OrdinalIgnoreCase)).OrderBy(d => d.LogDate).FirstOrDefault();
                    return logdata != null ? logdata.SessionId : null;
                }
                catch (SqlException)
                {
                    return null;
                }
            return null;
        }
        /// <summary>
        /// Inserts the specified log data.
        /// </summary>
        /// <param name="logData">The log data.</param>
        public void Insert(LogData logData)
        {
            if (logData != null)
                try
                {
                    LogDataDataAccess.Instance.Insert(logData);
                }
                catch (SqlException)
                {

                    return;
                }
        }
        /// <summary>
        /// Inserts the specified log data.
        /// </summary>
        /// <param name="logData">The log data.</param>
        /// <param name="announce">The announce.</param>
        public void Insert(LogData logData, out string announce)
        {
            if (logData != null)
                try
                {
                    announce = "Successfull!";
                    LogDataDataAccess.Instance.Insert(logData);
                }
                catch (SqlException exception)
                {
                    announce = exception.Message;
                    return;
                }
            else
            {
                announce = "Entities logdata is not null!";
            }

        }
        /// <summary>
        /// Deletes the specified session id.
        /// </summary>
        /// <param name="sessionId">The session id.</param>
        /// <param name="announce">The announce.</param>
        public void Delete(string sessionId, out string announce)
        {
            if (!string.IsNullOrEmpty(sessionId))
            try
            {
                announce = "Successfull!";
                LogDataDataAccess.Instance.DeleteLogDataBySession(sessionId);
            }
            catch (SqlException exception)
            {
                announce = exception.Message;
                return;
            }
            else
            {
                announce = "sessionId is not null";
            }
            
        }
        /// <summary>
        /// Deletes the specified session id.
        /// </summary>
        /// <param name="sessionId">The session id.</param>
        public void Delete(string sessionId)
        {
            try
            {

                LogDataDataAccess.Instance.DeleteLogDataBySession(sessionId);
            }
            catch (SqlException)
            {
                return;
            }
        }
    }
}
